
package net;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.InvalidPropertiesFormatException;
import java.util.Observable;

/**
 * Represents server thread. Every node in network has it's own server thread
 * which is needed to listen incoming connections from others nodes starting
 * conversation with the node.
 * @author Paweł
 */
public class ServerThread extends Observable implements Runnable {
    
    private ServerSocket serverSocket;

    /**
     * Creates ServerSocket and starts thread linsteng for incoming connections.
     * @throws InvalidPropertiesFormatException
     * @throws PropertiesFileNotFoundException - if properties file not found
     */
    public ServerThread() throws IOException {
        serverSocket = new ServerSocket(NetSettings.TCP_PORT);
    }

    public void run() {
        while (true) {
            try {
                Socket clientSocket = serverSocket.accept();
                setChanged();
                notifyObservers(clientSocket);
            } catch (IOException ex) {
                throw new RuntimeException(ex.getMessage(), ex);
            }
        }
    }

}
